package com.alibaba.system.controller;

import com.alibaba.basic.util.AjaxResult;
import com.alibaba.basic.util.PageList;
import com.alibaba.system.domain.Dictionarytype;
import com.alibaba.system.query.DictionarytypeQuery;
import com.alibaba.system.service.IDictionarytypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionarytype")
public class DictionarytypeController {

    @Autowired
    private IDictionarytypeService dictionarytypeService;

    /**
     * 接口：查询单个对象
     * @param id
     * @return
     */
    //http://localhost:8080/dictionarytype/1 + get
    @GetMapping("/{id}")
    public Dictionarytype findOne(@PathVariable("id")Long id){
        return dictionarytypeService.findById(id);
    }

    /**
     * 接口：查询所有对象
     * @return
     */
    //http://localhost:8080/dictionarytype + 无 + get
    @GetMapping
    public List<Dictionarytype> findAll(){
        return dictionarytypeService.findAll();
    }

    /**
     * 接口：分页查询
     * @param query
     * @return
     */
    //http://localhost:8080/dictionarytype + {json对象} + post

    @PostMapping
    public PageList<Dictionarytype> queryPage(@RequestBody DictionarytypeQuery query){
        return dictionarytypeService.queryPage(query);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    //http://localhost:8080/dictionarytype/1 + delete
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            dictionarytypeService.del(id);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     * 接口：添加或修改
     * @param dictionarytype
     * @return
     */
    @ApiOperation(value = "添加或修改")
    //http://localhost:8080/dictionarytype + {json对象} + put
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionarytype dictionarytype){
        try {
            if(dictionarytype.getId() == null){//添加
                dictionarytypeService.add(dictionarytype);
            }else{
                dictionarytypeService.update(dictionarytype);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }



    /**
     * 接口：批量删除
     * @param
     * @return
     */
    @PatchMapping
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            dictionarytypeService.patchDelete(ids);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

}
